package cn.ml.mybatisdemo;

import cn.ml.mybatisdemo.entity.ParamObj;
import cn.ml.mybatisdemo.entity.Person;
import cn.ml.mybatisdemo.dao.PersonDao;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class TestPerson {
    @Test
    public void foo() throws IOException {
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis.cfg.xml"));
        //connection
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 1.基于Mapper接口方式操作数据
        PersonDao dao = sqlSession.getMapper(PersonDao.class);

        List<Person> people4 = dao.list4("z","n");
        for (Person s:people4){
            System.out.println(s);
        }

    }

    public static void main(String[] args) throws IOException {
        //SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis.cfg.xml"));
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis.cfg.xml"));
        //connection
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 1.基于Mapper接口方式操作数据
        PersonDao dao = sqlSession.getMapper(PersonDao.class);

        System.out.println("****************list111*****************");
        List<Person> peoples = dao.list();
        for(Person p:peoples){
            System.out.println(p);
        }
        System.out.println("****************list111--使用分页插件后*****************");
        //可直接设置第几页和每页条数
        PageHelper.startPage(1,7);
        List<Person> peoplesPages = dao.list();
        for(Person p:peoplesPages){
            System.out.println(p);
        }
        System.out.println("****************list222*****************");
        Map params = new HashMap();
        params.put("likeName","%z%"); //map中的key与xml中list2的sql语句占位符相对应
        params.put("gender","n");
        List<Person> people2 = dao.list2(params);
        for (Person s:people2){
            System.out.println(s);
        }
        System.out.println("****************list333*****************");
        ParamObj paramObj = new ParamObj();
        paramObj.setLikeName("%z%");
        paramObj.setGender("n");
        List<Person> people3 = dao.list3(paramObj);
        for (Person s:people3){
            System.out.println(s);
        }
        System.out.println("****************list444*****************");
        List<Person> people4 = dao.list4("%z%","n");
        for (Person s:people4){
            System.out.println(s);
        }
        System.out.println("****************list555*****************");
        for(Person p:dao.list5()){
            System.out.println(p);
        }


        System.out.println("****************update111*****************");
        Person person = new Person("龙2","男");
        dao.save(person);
        sqlSession.commit();
        System.out.println("****************update111*****************");
        //当不传入sex时，结果会将id=17的sex改为null所以需要
        Person pUpdate = new Person();
        pUpdate.setName("磊磊2");
        pUpdate.setId(17);
        dao.update(pUpdate);
        sqlSession.commit();//提交事务

        System.out.println("****************update222*****************");
        //使用update2
        Person pUpdate2 = new Person();
        pUpdate2.setName("赵柳2");
        pUpdate.setId(18);
        dao.update2(pUpdate);
        sqlSession.commit();//提交事务
        sqlSession.close();//关闭sqlSession

    }
}
